<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Sentinel 哨兵 | Oliver知识收集站</title>
    <meta name="generator" content="VuePress 1.9.7">
    
    <meta name="description" content="享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css" as="style"><link rel="preload" href="/oliver-vuepress/assets/js/app.c21e6ffc.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/3.6dd9a2a1.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/1.898920d0.js" as="script"><link rel="preload" href="/oliver-vuepress/assets/js/62.8c5ef01e.js" as="script"><link rel="prefetch" href="/oliver-vuepress/assets/js/10.41b2bf91.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/11.a95c117d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/12.8607f0e1.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/13.a52d6846.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/14.249b4e52.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/15.d458d12e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/16.ba334206.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/17.1b91c9fa.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/18.e2ea2eb5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/19.bf0e2553.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/20.268bd174.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/21.cd1bbed5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/22.da4bc7f7.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/23.12f0c72f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/24.b7886742.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/25.6e71af85.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/26.5c127243.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/27.e98fd8bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/28.ce83b09c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/29.50398f0f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/30.05e1339c.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/31.ef4b13fb.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/32.ba5f8351.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/33.3902db0a.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/34.36a05884.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/35.87215872.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/36.db360c58.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/37.402e5374.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/38.c9228dd8.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/39.72ba5d1f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/4.7bb03d47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/40.7e7949bf.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/41.c0d5b947.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/42.d9984467.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/43.e6a43668.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/44.10d7fe47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/45.f692ec2d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/46.9b920343.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/47.8e3d94f9.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/48.7d356e5b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/49.b0df6271.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/5.1fa544da.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/50.805e1466.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/51.1b31d40e.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/52.44e69a41.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/53.da1def53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/54.6569f7db.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/55.5fc3de47.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/56.da649377.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/57.6ff15ed4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/58.a62f6424.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/59.f68ae517.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/6.f5bd8e9b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/60.dda416bc.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/61.4e0c719f.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/63.7089eb8b.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/64.b5ec150d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/65.6720cda4.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/66.4ee90e29.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/67.cc4b0c6d.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/7.d5950c53.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/8.382fb3a5.js"><link rel="prefetch" href="/oliver-vuepress/assets/js/9.d593f4c1.js">
    <link rel="stylesheet" href="/oliver-vuepress/assets/css/0.styles.4ea20d86.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-130b300a><div data-v-130b300a><div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-130b300a data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Oliver知识收集站</h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2>享受着互联网广泛知识，并加以记录，日积月累让它成为一个档案处！</p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div class="hide" data-v-130b300a><header class="navbar" data-v-130b300a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/oliver-vuepress/" class="home-link router-link-active"><!----> <span class="site-name">Oliver知识收集站</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-130b300a></div> <aside class="sidebar" data-v-130b300a><div class="personal-info-wrapper" data-v-39576ba9 data-v-130b300a><!----> <h3 class="name" data-v-39576ba9>
    oliver.shi
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>52</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>6</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/oliver-vuepress/" class="nav-link"><i class="undefined"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      Java
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/basics/" class="nav-link"><i class="undefined"></i>
  基础
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/concurrent/" class="nav-link"><i class="undefined"></i>
  并发
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/jvm/jvm.html" class="nav-link"><i class="undefined"></i>
  JVM
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/java/other/" class="nav-link"><i class="undefined"></i>
  杂
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/spring/first.html" class="nav-link"><i class="undefined"></i>
  Spring
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      中间件
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/redis/redis.html" class="nav-link"><i class="undefined"></i>
  Redis
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/kafka/framework.html" class="nav-link"><i class="undefined"></i>
  Kafka
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/middleware/zookeeper.html" class="nav-link"><i class="undefined"></i>
  Zookeeper
</a></li></ul></div></div><div class="nav-item"><a href="/oliver-vuepress/articles/algorithm/" class="nav-link"><i class="undefined"></i>
  算法
</a></div><div class="nav-item"><a href="/oliver-vuepress/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  TimeLine
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="undefined"></i>
      收集站
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/article/first.html" class="nav-link"><i class="undefined"></i>
  技术好文
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/book/first.html" class="nav-link"><i class="undefined"></i>
  书籍
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/assembly/first.html" class="nav-link"><i class="undefined"></i>
  优秀开发组件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/software/first.html" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/oliver-vuepress/articles/collect/plugin/first.html" class="nav-link"><i class="undefined"></i>
  插件
</a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-130b300a><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>Sentinel 哨兵</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>oliver.shi</span>
            
          <!---->
          2022
        </a></span></div></div> <div data-v-130b300a><main class="page"><section><div class="page-title"><h1 class="title">Sentinel 哨兵</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>oliver.shi</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>3/1/2020</span></i> <!----> <i class="tags iconfont reco-tag" data-v-f875f3fc><span class="tag-item" data-v-f875f3fc>Redis</span></i></div></div> <div class="theme-reco-content content__default"><blockquote><p><code>Sentinal(哨兵)</code>  是Redis的高可用解决方案：是由一个 或 多个 <code>Sentinal实例</code> 组成的 Sentinel 系统</p></blockquote> <ul><li>一个哨兵哨兵实例，也可以保证服务，但是为了服务的健壮性，至少需要3个实例</li> <li>哨兵 + Redis 主从的部署架构，是<strong>不保证数据零丢失</strong>的，只能保证 Redis 集群的高可用性。</li></ul> <p><strong>作用</strong></p> <ul><li>集群监控：负责监控 Redis master 和 slave 进程是否正常工作。</li></ul> <ul><li>消息通知：如果某个 Redis 实例有故障，那么哨兵负责发送消息作为报警通知给管理员。</li> <li>故障转移：如果 master node 挂掉了，会自动转移到 slave node 上。</li> <li>配置中心：如果故障转移发生了，通知 client 客户端新的 master 地址。</li></ul> <center><b>Sentinel案例图</b></center> <img src="/oliver-vuepress/middleware/redislearn/image-20220410103019964.png" alt="foo"> <h2 id="启动-sentinel"><a href="#启动-sentinel" class="header-anchor">#</a> 启动 Sentinel</h2> <blockquote><p><code>Redis-Sentinel</code>  是一台特殊的 Redis 服务器，支持的命令 和 普通的 Redis服务器不同
并且 无需持久化机制</p></blockquote> <p><strong>启动命令</strong></p> <div class="language-C extra-class"><pre class="language-c"><code>redis<span class="token operator">-</span>sentinel <span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>your<span class="token operator">/</span>sentinel<span class="token punctuation">.</span>conf

<span class="token comment">// 或</span>

redis<span class="token operator">-</span>server <span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>your<span class="token operator">/</span>sentinel<span class="token punctuation">.</span>conf <span class="token operator">--</span>sentinel
</code></pre></div><p><strong>配置文件相关参数</strong></p> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 哨兵sentinel监控的redis主节点的 ip port</span>
<span class="token comment"># master-name 可以自己命名的主节点名字 只能由字母A-z、数字0-9 、这三个字符&quot;.-_&quot;组成。</span>
<span class="token comment"># quorum 当这些quorum个数sentinel哨兵认为master主节点失联 那么这时 客观上认为主节点失联了</span>
<span class="token comment"># sentinel monitor &lt;master-name&gt; &lt;ip&gt; &lt;redis-port&gt; &lt;quorum&gt;</span>
sentinel monitor mymaster <span class="token number">127.0</span>.0.1 <span class="token number">6379</span> <span class="token number">2</span>

<span class="token comment"># 当在Redis实例中开启了requirepass foobared 授权密码 这样所有连接Redis实例的客户端都要提供密码</span>
<span class="token comment"># 设置哨兵sentinel 连接主从的密码 注意必须为主从设置一样的验证密码</span>
<span class="token comment"># sentinel auth-pass &lt;master-name&gt; &lt;password&gt;</span>
sentinel auth-pass mymaster MySUPER--secret-0123passw0rd

<span class="token comment"># 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒，改成3秒</span>
<span class="token comment"># sentinel down-after-milliseconds &lt;master-name&gt; &lt;milliseconds&gt;</span>
sentinel down-after-milliseconds mymaster <span class="token number">3000</span>

<span class="token comment"># 这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行 同步，这个数字越小，完成failover所需的时间就越长，但是如果这个数字越大，就意味着越 多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一个slave 处于不能处理命令请求的状态。</span>
<span class="token comment"># sentinel parallel-syncs &lt;master-name&gt; &lt;numslaves&gt;</span>
sentinel parallel-syncs mymaster <span class="token number">1</span>

<span class="token comment"># 故障转移的超时时间 failover-timeout 可以用在以下这些方面：</span>
<span class="token comment">#1. 同一个sentinel对同一个master两次failover之间的间隔时间。</span>
<span class="token comment">#2. 当一个slave从一个错误的master那里同步数据开始计算时间。直到slave被纠正为向正确的master那里同步数据时。</span>
<span class="token comment">#3.当想要取消一个正在进行的failover所需要的时间。</span>
<span class="token comment">#4.当进行failover时，配置所有slaves指向新的master所需的最大时间。不过，即使过了这个超时，slaves依然会被正确配置为指向master，但是就不按parallel-syncs所配置的规则来了</span>
<span class="token comment"># 默认三分钟</span>
<span class="token comment"># sentinel failover-timeout &lt;master-name&gt; &lt;milliseconds&gt;</span>
sentinel failover-timeout mymaster <span class="token number">180000</span>
</code></pre></div><h3 id="启动步骤"><a href="#启动步骤" class="header-anchor">#</a> 启动步骤</h3> <ol><li>初始化服务器</li> <li>Redis服务器，设置专用的Sentinel 标识</li> <li>初始化Sentinel 状态信息（即实例化SentinelState对象）</li> <li>初始化 Sentinel 监控的主服务器列表</li> <li>创建 主服务器的 网络连接</li></ol> <p><strong>1.初始化服务器</strong></p> <p>由于 和 正常 Redis 功能有所不同，在初始化的过程中，会去除掉一些在 Sentinel 模式下不使用的功能</p> <p>比如：载入 RDB 和 AOF 文件</p> <p><strong>2.Redis服务器，设置专用的Sentinel 标识</strong></p> <p>1、设置 Sentinel 对应的 端口号 （sentinel.c/REDIS_SENTINEL_PORT常量）</p> <p>2、设置对应的 Sentinel 命令表   （sentinel.c/sentinelcmds）</p> <p><strong>3.初始化Sentinel 状态信息（即实例化SentinelState对象）</strong></p> <p>创建一个 SentinelState 结构，用于保存服务器中所有的 Sentinel 功能相关的数据信息</p> <div class="language-c extra-class"><pre class="language-c"><code><span class="token keyword">struct</span> <span class="token class-name">sentinelState</span> <span class="token punctuation">{</span>     
  <span class="token comment">// 当前纪元,用于实现故障转移  </span>
	<span class="token class-name">uint64_t</span> current_epoch<span class="token punctuation">;</span>   
  
  <span class="token comment">// 保存了所有被这个sentinel 监视的主服务器 (key：服务器器名 value:sentinelRedisInstance (字典))</span>
	dict <span class="token operator">*</span>masters<span class="token punctuation">;</span>    
  
	<span class="token comment">// 是否进入了TILT 模式?    </span>
 	<span class="token keyword">int</span> tilt<span class="token punctuation">;</span>    
  
 	<span class="token comment">// 目前正在执行的脚本的数量  </span>
  <span class="token keyword">int</span> running_scripts<span class="token punctuation">;</span>   
  
	<span class="token comment">// 进入TILT 模式的时间    </span>
	<span class="token class-name">mstime_t</span> tilt_start_time<span class="token punctuation">;</span>   
  
	<span class="token comment">// 最后一次执行时间处理器的时间   </span>
	<span class="token class-name">mstime_t</span> previous_time<span class="token punctuation">;</span>  
  
	<span class="token comment">// 一个FIFO 队列,包含了所有需要执行的用户脚本    </span>
	list <span class="token operator">*</span>scripts_queue<span class="token punctuation">;</span>
 <span class="token punctuation">}</span> sentinel<span class="token punctuation">;</span>
</code></pre></div><p><strong>4.初始化 Sentinel 监控的主服务器列表</strong></p> <p>SentinelState 中 使用一个 字典去存储 所有的主服务器信息列表</p> <ul><li>字典键：服务器名</li> <li>字典值： sentinel.c/sentinelRedisInstance结构</li></ul> <img src="/oliver-vuepress/middleware/redislearn/image-20220410111528575.png" alt="foo"> <div class="language-c extra-class"><pre class="language-c"><code><span class="token keyword">typedef</span> <span class="token keyword">struct</span> <span class="token class-name">sentinelRedisInstance</span> <span class="token punctuation">{</span>    
  
  <span class="token comment">// 标识值,记录了实例的类型,以及该实例的当前状态  </span>
  <span class="token keyword">int</span> flags<span class="token punctuation">;</span>   
  
  <span class="token comment">// 实例的名字  如果没有配置的话 默认： 格式为ip:port </span>
  <span class="token keyword">char</span> <span class="token operator">*</span>name<span class="token punctuation">;</span>   
  
  <span class="token comment">// 实例的运行ID     </span>
  <span class="token keyword">char</span> <span class="token operator">*</span>runid<span class="token punctuation">;</span>   
  
  <span class="token comment">// 配置纪元,用于实现故障转移  </span>
  <span class="token class-name">uint64_t</span> config_epoch<span class="token punctuation">;</span>     
  
  <span class="token comment">// 实例的地址   </span>
  sentinelAddr <span class="token operator">*</span>addr<span class="token punctuation">;</span>   
  
  <span class="token comment">// 实例无响应多少毫秒之后才会被判断为主观下线(subjectively down )   </span>
  <span class="token class-name">mstime_t</span> down_after_period<span class="token punctuation">;</span>
  
	<span class="token comment">// 判断这个实例为客观下线所需的支持投票数量  </span>
  <span class="token keyword">int</span> quorum<span class="token punctuation">;</span>   
  
  <span class="token comment">// 在执行故障转移操作时,可以同时对新的主服务器进行同步的从服务器数量   </span>
  <span class="token keyword">int</span> parallel_syncs<span class="token punctuation">;</span>  
  
  <span class="token comment">// 刷新故障迁移状态的最大时限  </span>
  <span class="token class-name">mstime_t</span> failover_timeout<span class="token punctuation">;</span>    
  
  <span class="token comment">// 从服务器列表</span>
  dict <span class="token operator">*</span>slaves<span class="token punctuation">;</span>   
<span class="token punctuation">}</span> sentinelRedisInstance<span class="token punctuation">;</span>
</code></pre></div><p><strong>5、创建 主服务器的 网络连接</strong></p> <p>Sentinel 会与 需要被监控的主服务器建立网络连接，一共会创建两个连接</p> <ul><li>命令连接：专门用于向主服务器发送命令，并获取的信息
<ul><li>获取主服务器的信息</li> <li>获取主服务器下的从服务器的信息</li></ul></li> <li>订阅连接：用于订阅主服务器 <code>__sentinel__:hello</code> 频道
<ul><li>以及定时向 主、从服务器发送心跳信息</li></ul></li></ul> <p><strong>问题1： 为什么有两个连接</strong></p> <ol><li>有一为了确保数据不会丢失，所以专门用一个订阅连接来接收频道信息</li></ol> <p><strong>问题2：Sentinel 实例之间为什么只有一个命令连接</strong></p> <p>Sentinel 与 Sentinel 不需要进行订阅连接，Sentinel 通过订阅 主服务器 和 从服务器 就可以感知到对方</p> <h2 id="sentinel-信息获取过程"><a href="#sentinel-信息获取过程" class="header-anchor">#</a> Sentinel 信息获取过程</h2> <ul><li><p>Sentinel 默认每隔10秒 向 主服务器发送  info 消息 获取主服务器 和其从服务器的信息</p></li> <li><p>与主服务器建立 命令连接 和 订阅连接</p></li> <li><p>通过 获取都的从服务器 在向从服务器发送 info 消息 获取从服务器的信息 并建立连接</p></li> <li><p>Sentinel每2s一次，向所有被监视的主服务器和从服务器所订阅的—sentinel—:hello频道上发送消息，消息中会携带Sentinel自身的信息和主服务器的信息。 【进行信息交互，判断自身和集群的信息是否一致】</p></li></ul> <h2 id="故障迁移"><a href="#故障迁移" class="header-anchor">#</a> 故障迁移</h2> <p><strong>主要分为一下3步</strong></p> <ol><li>判断是否下线</li> <li>哨兵Leader选举</li> <li>Leader进行故障转移
<ol><li>选举 Master 节点</li> <li>进行故障转移</li></ol></li></ol> <h3 id="判断下线"><a href="#判断下线" class="header-anchor">#</a> 判断下线</h3> <blockquote><p>判断服务器是否下线的分为2个阶段  <strong>主观下线</strong> 、 <strong>客观下线</strong></p></blockquote> <p><strong>检测是如何发起的:</strong></p> <p>Sentinel 每秒  向所有建立连接的服务器 发送一次 PING 命令进行心跳</p> <p>返回：</p> <ul><li>+PONG</li> <li>-LOADING</li> <li>-MASTERDOWN</li></ul> <p>其中一种，都任务服务正常，其他类型返回 或 指定时间内没有返回，则会认为无效</p> <p><strong>主观下线</strong></p> <p>Sentinel 通过在配置文件中的 <code>down-after-milliseconds</code> 选项，设置的多少毫秒内，没有返回有效的回复 或 超时，则认为是<code>主观下线</code> ，会对 实例结构中 <code>flags</code> 属性设置为 <code>SRI_S_DOWN</code> 标识</p> <p>问题1:  多个Sentinel设置的主观下线时长可能不同</p> <p>一个设置了5S, 一个设置了8s。Sentinel 则会当 两个Sentinel 都符合条件后，才会认为是主观下线</p> <p>即: 多个Sentinel 配置的短线时长 不一致时，只有两个都满足了，才认为是真的下线了</p> <p><strong>客观下线</strong></p> <blockquote><p>当服务器实例为 <code>主观下线</code> ：Sentinel 会发送 <code>SENTINEL is-master-down-by-addr</code> 命令，通知其他的哨兵进行判定该Redis是否已经下线，当判断下线的哨兵数 等于或超过 配置的数量，则认为是 <code>客观下线</code> ， 会对 实例结构中 <code>flags</code> 属性设置为 <code>SRI_O_DOWN</code> 标识</p></blockquote> <p><strong>相关配置： <code>quorum=1</code>   quorum  符合投票哨兵的个数</strong></p> <h3 id="哨兵-leader-选举"><a href="#哨兵-leader-选举" class="header-anchor">#</a> 哨兵 Leader 选举</h3> <blockquote><p>主服务器 判定为 客观下线时，就需要选举出一个 Leader ，由 Leader Sentinel 对下主服务器进行故障转移</p> <p>哨兵的Leader 选举使用了 Raft协议来解决分布式一致性问题</p></blockquote> <p>Sentinel 选举的过程：</p> <ol><li>某Sentinel认定master客观下线后，该Sentinel会先看看自己有没有投过票，如果自己已经投过票
给其他Sentinel了，在一定时间内自己就不会成为Leader。</li> <li>如果该Sentinel还没投过票，那么它就成为Candidate（候选人），并完成一下几件事情
<ul><li>更新故障转移状态为start</li> <li>当前epoch加1，相当于进入一个新term，在Sentinel中epoch就是Raft协议中的term。</li> <li>向其他节点发送 is-master-down-by-addr 命令请求投票。命令会带上自己的epoch。</li> <li>给自己投一票（leader、leader_epoch）</li></ul></li> <li>当其它哨兵收到此命令时，可以同意或者拒绝它成为领导者；（通过判断epoch）</li> <li>Candidate会不断的统计自己的票数，直到他发现认同他成为Leader的票数超过一半而且超过它配置的quorum，这时它就成为了Leader。</li> <li>如果这个过程出现多个sentinel成为领导者(或者说票数相同时)，则会等待一段时间重新选举。</li> <li>其他Sentinel等待Leader从slave选出master后，检测到新的master正常工作后，就会去掉客观下
线的标识，进行故障转移操作</li></ol> <h3 id="故障转移"><a href="#故障转移" class="header-anchor">#</a> 故障转移</h3> <p><strong>转移步骤</strong></p> <p>1、在已下线主服务器下的 所有从服务器中，选出一个服务器</p> <p>2、将 下线主服务器下的 所有从服务器，改为  复制 新的 主服务器</p> <p>3、将已下线主服务器设置为新的主服务器的从服务器</p> <p><strong>主服务器的选择</strong></p> <ol><li>过滤掉已经 下线 或 断连 的从服务器</li> <li>过滤掉 最近5秒内 没有回复过  Sentinel的从服务器</li> <li>过滤掉 失效master  和 slave  断开时间超过了 <code>down-after-milliseconds</code> 10秒 的服务 （即选出相对新的从服务）</li> <li>对 slave 进行排序选出优先级高的
<ol><li>多个相同优先级的服务器，那么看 复制的偏移量，偏移量越靠后，优先级越高</li> <li>多个优先级相同，偏移量相同，则看 run id ，优先选小的  （越小说明重启数越少）</li></ol></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新: </span> <span class="time">4/23/2022, 2:48:05 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#启动-sentinel" class="sidebar-link reco-side-启动-sentinel" data-v-cb1513f6>启动 Sentinel</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#启动步骤" class="sidebar-link reco-side-启动步骤" data-v-cb1513f6>启动步骤</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#sentinel-信息获取过程" class="sidebar-link reco-side-sentinel-信息获取过程" data-v-cb1513f6>Sentinel 信息获取过程</a></li><li class="level-2" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#故障迁移" class="sidebar-link reco-side-故障迁移" data-v-cb1513f6>故障迁移</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#判断下线" class="sidebar-link reco-side-判断下线" data-v-cb1513f6>判断下线</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#哨兵-leader-选举" class="sidebar-link reco-side-哨兵-leader-选举" data-v-cb1513f6>哨兵 Leader 选举</a></li><li class="level-3" data-v-cb1513f6><a href="/oliver-vuepress/articles/middleware/redis/Sentinel%E5%93%A8%E5%85%B5.html#故障转移" class="sidebar-link reco-side-故障转移" data-v-cb1513f6>故障转移</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/oliver-vuepress/assets/js/app.c21e6ffc.js" defer></script><script src="/oliver-vuepress/assets/js/3.6dd9a2a1.js" defer></script><script src="/oliver-vuepress/assets/js/1.898920d0.js" defer></script><script src="/oliver-vuepress/assets/js/62.8c5ef01e.js" defer></script>
  </body>
</html>
